/*年轻人，只管向前看，不要管自暴自弃者的话*/
package com.znb.okdown.flutter.android.view

import android.content.Context
import android.view.View
import android.widget.TextView
import com.znb.okdown.flutter.Constants
import com.znb.okdown.ui.view.AndroidView
import io.flutter.plugin.common.BinaryMessenger
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.platform.PlatformView


/**
 * create by itz on 2024/1/19 13:58
 * desc : AndroidView 控制器
 **/
class AndroidViewController(context: Context, messenger: BinaryMessenger, id: Int?, args: Any?) :
    PlatformView, MethodChannel.MethodCallHandler {

    private val androidView: AndroidView = AndroidView(context)
    private var methodChannel: MethodChannel

    init {
        //通信
        methodChannel = MethodChannel(messenger, Constants.METHOD_CHANNEL_NAME)
        methodChannel.setMethodCallHandler(this)
        if (args is Map<*, *>) {
            androidView.setData("我是安卓原生的TextView${args["androidView"]}")
        }
    }

    override fun getView(): View? {
        return androidView
    }

    override fun dispose() {

    }

    override fun onMethodCall(call: MethodCall, result: MethodChannel.Result) {
        when (call.method) {
            Constants.METHOD_NAME_FLT_TO_AND -> {
                val data = call.argument<String>("androidView")
                if (data != null) {
                    androidView.setData("我是安卓原生的TextView$data")
                    result.success("setData  success")
                } /*else {
                    result.error("-1", "androidView cannot be null", null)
                }*/
            }

            else -> {
                result.notImplemented()
            }
        }
    }
}